import {
  getRegistered,
  getUnRegister,
  getAllAdmin,
  getRolelist,
  registeredDelete,
  UnRegisterDelete,
  addAdmin,
  update,
  noupdate,
  daochu,
  chongfa,
  viewLogList,
  getSelection,
  searchPerson,
  getPositionList,
  deletePosition,
  positionSelect,
  addPosition,
  editPositon,
  getPost
} from '@/api/systemManage'
import { Message } from 'element-ui'

const oldState = { // 用于重置state数据
  // 系统用户管理
  tableData: [],
  params: {
    adminEmail: null,
    adminName: null,
    roleName: null,
    page: 0,
    size: 10
  },
  total: 0,
  // 管理员日志
  adminLogData: {
    tableData: [],
    total: 0
  },
  // 职位管理
  positionManageData: {
    tableData: []

  }
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_tableData (state, data) {
    state.tableData = data
  },
  set_page (state, data) {
    state.params.page = data - 1
  },
  set_size (state, data) {
    state.params.size = data
  },
  set_adminEmail (state, data) {
    state.params.adminEmail = data
  },
  set_roleName (state, data) {
    state.params.roleName = data
  },
  set_adminName (state, data) {
    state.params.adminName = data
  },
  set_total (state, data) {
    state.total = data
  },
  // 管理员日志
  set_adminTableData (state, list) {
    state.adminLogData.tableData = list
  },
  set_adminTotal (state, total) {
    state.adminLogData.total = total
  },
  // 职位管理
  set_positionManageData (state, list) {
    state.positionManageData.tableData = list
  }

}
const actions = {
  // 获取已注册信息表
  getRegisteredData ({
    state,
    commit
  }) {
    return new Promise((resolve, reject) => {
      getRegistered(state.params).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data.list)
          commit('set_total', res.data.data.size)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
          commit('set_total', 0)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 获取未注册信息表
  getUnRegisterData ({
    state,
    commit
  }) {
    return new Promise((resolve, reject) => {
      getUnRegister(state.params).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data.list)
          commit('set_total', res.data.data.size)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
          commit('set_total', 0)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 查看所有管理员信息表
  getAllAdminData ({
    state,
    commit
  }) {
    return new Promise((resolve, reject) => {
      getAllAdmin(state.params).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data.list)
          commit('set_total', res.data.data.size)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
          commit('set_total', 0)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 获取角色下拉框
  getRolelistData ({
    state,
    commit
  }) {
    return new Promise((resolve, reject) => {
      getRolelist().then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 已经注册表的删除
  registeredDeleteData ({
    state,
    commit
  }, playload) {
    return new Promise((resolve, reject) => {
      registeredDelete(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 未注册表的删除
  UnRegisterDeleteData ({
    state,
    commit
  }, playload) {
    return new Promise((resolve, reject) => {
      UnRegisterDelete(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 添加
  addAdminData ({
    state,
    commit
  }, playload) {
    return new Promise((resolve, reject) => {
      addAdmin(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data)
        }
      })
    })
  },
  // 已经注册表的编辑
  updateData ({
    state,
    commit
  }, playload) {
    return new Promise((resolve, reject) => {
      update(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 未注册表的编辑
  noupdateData ({
    state,
    commit
  }, playload) {
    return new Promise((resolve, reject) => {
      noupdate(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },

  // 导出
  DaoChu ({
    state,
    commit
  }, payload) {
    return new Promise((resolve, reject) => {
      daochu(payload).then(res => {
        // console.log(res)
        // resolve(res)
        // if (res.data.code === 200 && res.data) {
        //   // commit('set_tableData', res.data.data)
        //   resolve(res.data)
        // } else {
        //   reject(res.data.message)
        // }
        if (res.data.type === 'application/json') {
          const reader = new FileReader()
          reader.readAsText(res.data, 'utf-8')
          reader.onload = () => {
            const newRes = JSON.parse(reader.result)
            // console.log(newRes)
            Message({
              type: 'error',
              message: newRes.message,
              duration: 1000
            })
          }
        } else {
          // 正确回调函数
          resolve(res)
        }
      })
    })
  },
  // 重发
  chongfaData ({
    state,
    commit
  }, playload) {
    return new Promise((resolve, reject) => {
      chongfa(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },

  // 管理日志列表查看
  getLogList ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      viewLogList(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.data) {
          commit('set_adminTableData', res.data.data.list)
          commit('set_adminTotal', res.data.data.size)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 获取操作模块列表
  getOperationList ({ state }) {
    return new Promise((resolve, reject) => {
      getSelection().then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 模糊查询人名
  searchPerson ({ state }, payload) {
    return new Promise((resolve, reject) => {
      searchPerson(payload).then(res => {
        if (res.data && res.data.code === 200) {
          // console.log(res)
          resolve(res.data)
        } else {
          reject(res.data.code)
        }
      })
    })
  },
  // 获取职位列表
  getPostList () {
    return new Promise((resolve, reject) => {
      getPost().then(res => {
        if (res.data && res.data.code === 200) {
          resolve(res.data)
        } else {
          reject(res.data.code)
        }
      })
    })
  },
  // 职位管理列表查看
  getPositionListData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getPositionList(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.data) {
          commit('set_positionManageData', res.data.data.list)
          // commit('set_adminTotal', res.data.data.size)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 删除职位
  deletePositionData ({
    state,
    commit
  }, playload) {
    return new Promise((resolve, reject) => {
      deletePosition(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },

  // 先查看职位之间的从属关系
  positionSelectData ({ state }) {
    return new Promise((resolve, reject) => {
      positionSelect().then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 添加职位
  addPositionData ({ state }, playload) {
    return new Promise((resolve, reject) => {
      addPosition(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.message) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 编辑职位
  editPositonData ({ state }, playload) {
    return new Promise((resolve, reject) => {
      editPositon(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data.message) {
          resolve(res.data)
        } else {
          reject(res.message)
        }
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
